﻿using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WidgetsDesign.API.Dtos;
using WidgetsDesign.API.Models;
using WidgetsDesign.API.Services;

namespace WidgetsDesign.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class HitokotoController : ControllerBase
    {
        private readonly IHitokotoRepository _hitokotoRepository;
        private readonly IMapper _Mapper;
        public HitokotoController(IHitokotoRepository hitokotoRepository, IMapper mapper)
        {
            _hitokotoRepository = hitokotoRepository;
            _Mapper = mapper;
        }
        [HttpGet]
        public async Task<IActionResult> GetHitokoto([FromQuery] string type,int MaxSize,int MinSize )
        {
            string[] types = type.Split(',');
            var result = await _hitokotoRepository.GetHitokotoAsync(types, MaxSize, MinSize);
            if (result == null)
            {
                return Ok(new { Hitokoto="未找到该筛选条件相关的一言" });
            }   
            return Ok(result);
        }
        [HttpPost]
        public async Task<IActionResult> CreateHitokoto([FromBody] HitokotoDto hitokotoDto)
        {
            var HitokotoModel = _Mapper.Map<HitokotoModel>(hitokotoDto);

            if(await _hitokotoRepository.GetHitokotoExistsAsync(hitokotoDto.Id))
            {
                return BadRequest();
            }
            _hitokotoRepository.CreateHitokoto(HitokotoModel);
            await _hitokotoRepository.SaveAsync();
            return Ok();
        }
      
    }

}
